package com.me.modules.scheduling.controller;

import java.util.*;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.controller.BaseController;
import com.me.modules.common.page.Page;
import com.me.modules.common.utils.*;
import com.me.modules.sys.entity.GroupEntity;
import com.me.modules.sys.entity.UserEntity;
import com.me.modules.sys.service.GroupService;
import com.me.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.me.modules.scheduling.entity.SchedulingInfoEntity;
import com.me.modules.scheduling.service.SchedulingInfoService;

import javax.servlet.http.HttpServletRequest;


/**
 * 排班表
 *
 * @author me
 * @email me@163.com
 * @date 2019-01-28 14:29:35
 */
@Controller
@RequestMapping("/scheduling")
public class SchedulingInfoController extends BaseController{
	@Autowired
	private SchedulingInfoService schedulingInfoService;
	@Autowired
	private GroupService groupService;
	@Autowired
	private UserService userService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@SysLog("查看排班管理")
//	@RequiresPermissions("schedulinginfo:list")
	public String list(SchedulingInfoEntity schedulingInfoEntity, Model model, HttpServletRequest request){
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<SchedulingInfoEntity> page = schedulingInfoService.findPage(schedulingInfoEntity, pageNum);
		model.addAttribute("page", page);
		return "scheduling/schedulingList";
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info")
//	@RequiresPermissions("schedulinginfo:info")

	public String info(Model model , String id, HttpServletRequest request){
		SchedulingInfoEntity schedulingInfo = schedulingInfoService.queryObject(id);
		model.addAttribute("scheduling",schedulingInfo);
		List<GroupEntity> groupEntities = groupService.queryList(new HashMap<>());
		List groups = new ArrayList();
		Map<String,Object> groupMap;
		for (GroupEntity groupEntity : groupEntities) {
			groupMap = new HashMap<>();
			groupMap.put("name",groupEntity.getName());
			groupMap.put("value",groupEntity.getId());
			groups.add(groupMap);
		}

		model.addAttribute("groups",JsonUtil.getJsonByObj(groups));
		return "scheduling/schedulingEdit";
	}

	/**
	 * 保存
	 */
	@RequestMapping("/edit")
//	@RequiresPermissions("schedulinginfo:save")
	@ResponseBody
	public Result edit(SchedulingInfoEntity schedulingInfo,String days){
		String[] strings = days.split(", ");//01/09/2019, 01/11/2019
			if(StringUtils.isEmpty(schedulingInfo.getId())){
				for (String string : strings) {
					Date date = DateConverUtil.getDbyST(string, "MM/dd/yyyy");
					SchedulingInfoEntity scheduling = new SchedulingInfoEntity();
					scheduling.setDutyFree(schedulingInfo.getDutyFree());
					schedulingInfo.setBeginDate(date);
					schedulingInfo.setEndDate(date);
					schedulingInfo.setStatus("0");
					schedulingInfo.setCreateDate(new Date());
					schedulingInfo.setCreateUser(UserUtils.getCurrentUser().getId());
					schedulingInfo.setLastModifyDate(new Date());
					schedulingInfo.setLastModifyUser(UserUtils.getCurrentUser().getId());
					schedulingInfoService.save(schedulingInfo);
				}
			}else{
//				schedulingInfo.setBeginDate(date);
//				schedulingInfo.setEndDate(date);
				schedulingInfo.setLastModifyDate(new Date());
				schedulingInfo.setLastModifyUser(UserUtils.getCurrentUser().getId());
				schedulingInfoService.update(schedulingInfo);
			}


		return Result.ok();

	}


	/**
	 * 删除
	 */
	@RequestMapping("/delete")
//	@RequiresPermissions("schedulinginfo:delete")
	@ResponseBody
	@SysLog("删除排班")
	public Result delete(String id){
		schedulingInfoService.delete(id);

		return Result.ok();
	}

	@RequestMapping("/getGroupUser")
//	@RequiresPermissions("schedulinginfo:delete")
	@ResponseBody
	public Result getGroupUser(String groupId){

		List<UserEntity> userEntityList = userService.queryUserListByGroupId(groupId);
		return Result.ok().put("users",userEntityList);
	}

	@RequestMapping("/calendarSchedList")
//	@RequiresPermissions("schedulinginfo:delete")
	@SysLog("查看排班日历")
	public String calendarSchedList(Model model, String groupId){
		List<SchedulingInfoEntity> schedulingInfoList = schedulingInfoService.queryCalendarList();
		List<List<String>> days = DateConverUtil.getMonthDays(1,1);
		for (List<String> day : days) {
			for (SchedulingInfoEntity schedulingInfo : schedulingInfoList) {
				schedulingInfo.getBeginDate();
				String schDate = DateConverUtil.getSbyDT(schedulingInfo.getBeginDate(), "yyyy-MM-dd");
				if(day.get(0).equals(schDate)){
					if(StringUtils.isEmpty(day.get(1))){
						day.set(1, schedulingInfo.getUserName());
					}else {
						day.set(1, day.get(1)+","+schedulingInfo.getUserName());
					}
				}
			}
		}
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR); //获得当前年
		int month = calendar.get(Calendar.MONTH) + 1;   //获取月份，0表示1月份
		model.addAttribute("days",JsonUtil.getJsonByObj(days));
		model.addAttribute("year",year);
		model.addAttribute("month",month);

		return "scheduling/calendarSchedList";
	}


}
